//
//  HZHomeRetweetView.swift
//  HansWeibo
//
//  Created by Hans on 16/8/13.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit
import SnapKit
class HZHomeRetweetView: UIView {

    
    var retweetViewBottomConstraint: Constraint?
    
    
    // 定义一个ViewModel 供外界赋值
    var homeViewModel: HZHomeViewModel?{
        didSet{
            // 测试文字
            contentLabel.text = homeViewModel?.homeModel?.retweeted_status?.text
            
            // 卸载约束
            retweetViewBottomConstraint?.uninstall()
            // 判断是否有配图
            // 有配图
            if homeViewModel?.homeModel?.retweeted_status?.pic_urls?.count > 0 {
                // 赋值
                pictureView.PictureInfo = homeViewModel?.homeModel?.retweeted_status?.pic_urls
                // 更新约束
                self.snp_makeConstraints { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                }
                // 显示配图
                pictureView.hidden = false
            }else {
                // 没有配图
                // 更新约束
                self.snp_makeConstraints { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
                // 隐藏配图
                pictureView.hidden = true
            }
            
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        // 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        contentLabel.snp_makeConstraints { (make) in
            make.top.leading.equalTo(10)
        }
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo((SCREENW-250)/2)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        self.snp_makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
    
    
    // MARK - 懒加载控件
    // 转发微博
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, maxWidth: SCREENW - 20)
    // 配图
    private lazy var pictureView: HZHomePictureView = HZHomePictureView()
}
